/*
 * @(#) SendEmailThread
 *
 * <br> Copyright:  Copyright (c) 2017
 * <br> @author caijinhuang
 * <br> 2017-12-18 16:16:47
 */

package com.sunsharing.sharemeeting.common;

import com.sunsharing.sharemeeting.dao.api.DecisionDao;
import com.sunsharing.sharemeeting.dao.api.DecisionPerformerDao;
import com.sunsharing.sharemeeting.entity.Decision;

import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import com.sunsharing.sharemeeting.entity.DecisionPerformer;
import com.sunsharing.sharemeeting.util.SendEmail;

/**
 * 发送邮件的线程
 */
@Component
public class SendEmailThread extends Thread {
    @Resource
    DecisionDao decisionDao;
    @Resource
    DecisionPerformerDao decisionPerformerDao;

    public SendEmailThread() throws InterruptedException {
        Thread thread = new Thread(this);
        thread.start();
    }

    @Override
    public void run() {
        /*查询所有未完成的决议*/
        while (true) {
            if (decisionDao != null) {
                System.out.println("执行");
                List<Decision> decisionList = decisionDao.selectDecision();
                for (Decision d : decisionList) {
                    Date now = new Date();
                    Date old = d.getDecisionEndTime();
                    long lastRemind = 0;
                    long remaindDay = 0;
                    if (d.getRemindTime() != null) {
                        Date remindTime = d.getRemindTime();
                        lastRemind = now.getTime() - remindTime.getTime();//最后提醒时间差
                        remaindDay = lastRemind / (24 * 60 * 60 * 1000);//最后提醒相隔天数
                    }
                    long l = now.getTime() - old.getTime();//超时时间差
                    long day = l / (24 * 60 * 60 * 1000);//任务超时天数
                    long second = l / (1000);//超时秒数
                    /*第一次超时提醒,每10分钟线程轮询一次，线程超时在0-10钟之内的邮件会被第一次提醒，之后每天（24小时）提醒一次*/
                    if (second > 0 && second < 60000L) {
                        SendEmail.sample(d.getEmail(), d.getNickname() + ":您的任务【" + d.getDecisionContent() + "】已超时请即使处理。");
                        decisionDao.updateDecisionState(d.getNoteDecisionId());
                        DecisionPerformer decisionPerformer = new DecisionPerformer();
                        decisionPerformer.setDecisionPerformerId(d.getDecisionPerformerId());
                        decisionPerformer.setRemindTime(now);
                        decisionPerformerDao.updateDecisionPerformer(decisionPerformer);//更新最后提醒时间
                    }
                    /**
                     * 之后每24小时提醒一次
                     */
                    if (day >= 1 && remaindDay >= 1) {
                        SendEmail.sample(d.getEmail(), d.getNickname() + ":您的有超时任务【" + d.getDecisionContent() + "】未完成，请及时处理");
                        DecisionPerformer decisionPerformer = new DecisionPerformer();
                        decisionPerformer.setDecisionPerformerId(d.getDecisionPerformerId());
                        decisionPerformer.setRemindTime(now);
                        decisionPerformerDao.updateDecisionPerformer(decisionPerformer);//更新最后提醒时间
                        decisionDao.updateDecisionState(d.getNoteDecisionId());
                    }
                }
                try {
                    /*线程休眠1分钟*/
                    Thread.sleep(60000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
